package com.education.dao.impl;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.education.dao.AccountDAO;
import com.education.hibernate.Account;
 
public class AccountDAOImpl extends HibernateDaoSupport implements AccountDAO{
	

	//add the customer
	public boolean addAccount(Account account)
	{
		try
		{ 
			getHibernateTemplate().save(account); 	
		}
		catch (DataAccessException e) 
		{ 
			System.out.println("Exception when save in database!");
			return false;
		} 
		System.out.println("Add a customer in db!");
		return true;
	}
	public boolean  isAccountExist(String username)
	{	
		String hql = "from Account u where u.account=?";
		List userList = null;
		try
		{
			userList = this.getHibernateTemplate().find(hql, username); 
		}
		catch (DataAccessException e) 
		{ 
			System.out.println("Exception when find data in database!");
			return false;
		} 
		finally
		{
			if (userList.size() > 0) {
				System.out.println("this account has already in db!");
				return true;
			}
			return false;
		}
	}
	public boolean isValidUser(String username, String password) 
	{
		String hql = "from Account u where u.account=? and u.password=?";
		String []userInfo=new String[2];
		userInfo[0]=username;
		userInfo[1]=password;
		List userList = this.getHibernateTemplate().find(hql, userInfo);
		if (userList.size() > 0) {
			return true;
		}
		return false;
	}
	public boolean  updateAccount(Account account)
	{
		return true;
	}
	public boolean  deleteAccount(Account account)
	{
		return true;
	}
	public boolean  deleteAll(Account account)
	{
		return true;
	}
}